package com.it2297.model;

import java.sql.PreparedStatement;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

import com.it2297.main.App;

public class Payment {

	public Payment() {

	}

	public Payment(int payment_id, String status, String paymentDesc,
			double amount, String DatePaid, String location) {
		this.payment_id = payment_id;
		this.status = status;
		this.amount = amount;
		this.DatePaid = DatePaid;
		this.location = location;
	}

	private int payment_id;
	private double amount;
	private String status;
	private String DatePaid;
	private String location;
	private Timestamp date;

	public double getAmount() {
		return amount;
	}

	public void setAmount(double amount) {
		this.amount = amount;
	}

	public String getDatePaid() {
		return DatePaid;
	}

	public void setDatePaid(String datePaid) {
		DatePaid = datePaid;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public int getPayment_id() {
		return payment_id;
	}

	public void setPayment_id(int payment_id) {
		this.payment_id = payment_id;
	}

	public Payment getPayment(int id) {
		Payment payment = new Payment();
		try {
			App.mySqlConnection.open();
			String sql = "SELECT * FROM americano.payment WHERE payment_id =? AND status = 'unpaid'";
			PreparedStatement ps = App.mySqlConnection.getConnection().prepareStatement(sql);
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();
			if (rs.next()){
				payment.setAttributes(rs);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			App.mySqlConnection.close();
		}
		return payment;
	}
	
	public void DeletePayment(int id){
		try {
			App.mySqlConnection.open();
			String sql = "DELETE  FROM americano.payment WHERE payment_id =?";
			PreparedStatement ps = App.mySqlConnection.getConnection().prepareStatement(sql);
			ps.setInt(1, id);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			App.mySqlConnection.close();
		}
	}
	
	public ArrayList<Payment> getAllPayment(){
		ArrayList<Payment> pay = new ArrayList<Payment>();
		try {
			App.mySqlConnection.open();
			String sql = "SELECT * FROM americano.payment WHERE status='unpaid'";
			PreparedStatement ps = App.mySqlConnection.getConnection().prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				Payment payment = new Payment();
				payment.setAttributes(rs);
				pay.add(payment);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			App.mySqlConnection.close();
		}
		return pay;
	}
	
	public Payment getPaymentHistory(int id) {
		Payment payment = new Payment();
		try {
			App.mySqlConnection.open();
			String sql = "SELECT * FROM americano.payment WHERE payment_id =?";
			PreparedStatement ps = App.mySqlConnection.getConnection().prepareStatement(sql);
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();
			if (rs.next()){
				payment.setAttributes(rs);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			App.mySqlConnection.close();
		}
		return payment;
	}
	
	public Payment getPaymentRecord(int id) {
		Payment payment = new Payment();
		try {
			App.mySqlConnection.open();
			String sql = "SELECT * FROM americano.payment WHERE payment_id =?";
			PreparedStatement ps = App.mySqlConnection.getConnection().prepareStatement(sql);
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();
			if (rs.next()){
				payment.setAttributes(rs);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			App.mySqlConnection.close();
		}
		return payment;
	}
	
	public boolean makePayment(int id){
		boolean valid = false;
		try {
			App.mySqlConnection.open();
			String sql = "UPDATE americano.payment set status = ? , date_payment = ?where payment_id=?";
			PreparedStatement ps = App.mySqlConnection.getConnection().prepareStatement(sql);
			ps.setString(1, "paid");
			ps.setTimestamp(2, getDate());
			ps.setInt(3, id);
			ps.executeUpdate();
			
			valid = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			App.mySqlConnection.close();
		}
		return valid;
	}
	
	

	public int addPayment(Payment p) {
		int id = 0;

		try {
			App.mySqlConnection.open();
			String sql = "INSERT INTO americano.payment(amount,status)";
			sql += "VALUES(?,?)";
			PreparedStatement ps = App.mySqlConnection.getConnection()
					.prepareStatement(sql);
			ps.setDouble(1, p.getAmount());
			ps.setString(2, "unpaid");

			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			App.mySqlConnection.close();
		}

		try {
			App.mySqlConnection.open();
			String sql = "SELECT LAST_INSERT_ID() AS 'ID'";
			PreparedStatement ps = App.mySqlConnection.getConnection()
					.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				id = rs.getInt("ID");
				System.out.println(id);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			App.mySqlConnection.close();
		}
		return id;
	}

	public void updatePayment(int id) {
		try {
			App.mySqlConnection.open();
			String sql = "UPDATE americano.payment set status =? where payment =? ";
			PreparedStatement ps = App.mySqlConnection.getConnection()
					.prepareStatement(sql);
			ps.setInt(2, id);
			ps.setString(1, "paid");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			App.mySqlConnection.close();
		}

	}

	public void cancelPayment(int id) {
		try {
			App.mySqlConnection.open();
			String sql = "DELETE * FROM americano.payment where payment_id =?";
			PreparedStatement ps = App.mySqlConnection.getConnection()
					.prepareStatement(sql);
			ps.setInt(1, id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			App.mySqlConnection.close();
		}

	}

	private void setAttributes(ResultSet rs) {
		try {
			this.setPayment_id(rs.getInt("payment_id"));
			this.setAmount(rs.getDouble("amount"));
			this.setDatePaid(rs.getString("date_payment"));
			this.setLocation(rs.getString("location"));
			this.setStatus(rs.getString("status"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Timestamp getDate() {
		 java.util.Date date= new java.util.Date();
		return new Timestamp(date.getTime());
	}

}
